import javax.swing.JOptionPane;


	class App {
		public static void main(String[] args) {

		Phonebook phonebook;
		String statusmessage;		//Stores last printed message so it can be printed in two places
		String instruction;			//Instruction inputed by user

		String size = JOptionPane.showInputDialog("Hello,\nHow many entries do you want your phonebook to store?\n\n");
		int intsize = Integer.parseInt(size);
		
		while(intsize <= 0 || intsize > 15000000){	//Array can not have 0 elements and cannot have more than 15million elements.
			size = JOptionPane.showInputDialog("Input invalid, please enter a number greater than 0 and less than 15 million");
			intsize = Integer.parseInt(size);
		}
		phonebook = new Phonebook(intsize);	//Number is for size of phonebook
		

		
		do{
			instruction = (JOptionPane.showInputDialog("What do you want to do?\n  Enter 1 to add new contact,\n  Enter 2 to view a contact,\n  Enter 3 to view all contacts,\n  Enter 4 to delete a contact,\n  Enter 5 to search,\n  Enter 9 to stop program\n\n"));
			
			/*Instructions:
			 * 1: Add new contact
			 * 2: View a contact
			 * 3: View all contacts
			 * 4: Delete a contact
			 * 5: Search
			 * 9: Exit Program*/
			
			//Add contact
			if(instruction.equals("1")){
		
		Contact tempcontact;
		//Get contact details
		tempcontact = new Contact(JOptionPane.showInputDialog("First Name: "), JOptionPane.showInputDialog("Second Name: "), 
				JOptionPane.showInputDialog("Phone Number: "), JOptionPane.showInputDialog("Address: "));
		
		//Check that only valid characters were used
		if(tempcontact.getFirstName().matches("[a-zA-Z]+") && tempcontact.getSecondName().matches("['a-zA-Z\\s]+")
				&& tempcontact.getNumber().matches("[0-9]+")&& tempcontact.getAddress().matches("[.,a-zA-Z0-9\\s]+")){

			
			statusmessage = phonebook.addContact(tempcontact);	//Add contact and store returned result
			System.out.println(statusmessage);			//Print result in system.out
			JOptionPane.showMessageDialog(null,statusmessage,"Add Contact",JOptionPane.INFORMATION_MESSAGE);	//Print result
			
		}

		else{
		//Print error message
		statusmessage = ("Invalid characters used");
		System.out.println(statusmessage);
		JOptionPane.showMessageDialog(null,statusmessage,"Error",JOptionPane.ERROR_MESSAGE);
		}
		
			}
			
			//Find contact by entry number
			else if(instruction.equals("2")){
				String temp = JOptionPane.showInputDialog("Enter entry number of contact\n");
				int tempint = Integer.parseInt(temp) - 1;//Used to parse array location
				statusmessage = phonebook.getContactDetails(tempint);//Stores result
				//Print Result
				System.out.println(statusmessage);
				JOptionPane.showMessageDialog(null,statusmessage,"Contact Details",JOptionPane.INFORMATION_MESSAGE);
			}
			
				
			//View all contacts
			 else if(instruction.equals("3")){
				for (int i = 0;i<= phonebook.getSize() - 1;i++){						
					if(!phonebook.checkNull(i)){//If not null
					statusmessage = phonebook.getContactDetails(i);		//Status message = contact details
					//Print results
					System.out.println(statusmessage);
					JOptionPane.showMessageDialog(null,statusmessage,"Contact Details",JOptionPane.INFORMATION_MESSAGE);
					}
				}
			}
			
			//Delete a contact
			 else if(instruction.equals("4")){
				String temp = JOptionPane.showInputDialog("Enter entry number you wish to delete: ");
				int tempint = Integer.parseInt(temp) - 1;//Used to parse array location
				statusmessage =	phonebook.deleteContact(tempint); //Delete contact at location tempint
					//Print result
					System.out.println(statusmessage);
					JOptionPane.showMessageDialog(null,statusmessage,"Delete Contact",JOptionPane.INFORMATION_MESSAGE);
			}
			
				
				//Search
			 else if(instruction.equals("5")){
				 //Choose to search by first name, second name, number or address
				 String option = JOptionPane.showInputDialog("Enter 1 to search by First Name,\nEnter 2 to search by Second Name,\nEnter 3 to search by Phone Number,\nEnter 4 to search by Address");
				 //Make sure a valid option was chosen
				 if(option.equals("1") || option.equals("2") || option.equals("3") || option.equals("4")){
				 String search = JOptionPane.showInputDialog("Enter search: ");
				 Contact[] result = phonebook.search(option,search);//Store result as contact array
				 
				 if(result == null){
					 statusmessage = ("No matches found"); //If array returned is null, then no results
					//Print 'no results' message
					 System.out.println(statusmessage);
					 JOptionPane.showMessageDialog(null,statusmessage,"Result",JOptionPane.INFORMATION_MESSAGE);
				 }
				 
				 else{//Else if there are results
				 for(int i = 0;i<= result.length - 1;i++){//Iterate through results array
					 /*Results array is copy of phonebook except all non-matches are excluded
					  * The reason for this is so that the actual entry position can be returned
					  * This is useful if the user wants to delete an entry, and needs to know their entry number*/
					 if(!(result[i] == null)){
					 statusmessage = result[i].getDetails();
					 //Print results
					 System.out.println(statusmessage);
					 JOptionPane.showMessageDialog(null,statusmessage,"Entry #" + (i + 1),JOptionPane.INFORMATION_MESSAGE);
					 }
				 }
				 }
				 }
				 
			 }
		
		
		}while(!instruction.equals("9"));//If user enters '9', the program stops
		
		
		}
}
